package edu.ielab.localansys.ui;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;

public class Table_Model extends AbstractTableModel {

	private static final long serialVersionUID = -6948600277840750674L;

	private Vector content = null;

	private String[] title_name = { "Name", "Value", "Description" };

	public Table_Model() {
		content = new Vector();
	}

	public Table_Model(int count) {
		content = new Vector(count);
	}

	public void addRow(String name, String value, String des) {
		Vector v = new Vector(3);
		v.add(0, name);
		v.add(1, value);
		v.add(2, des);
		content.add(v);
	}
	
	public void addRow(TripleRowContent trc) {
		addRow(trc.getName(), trc.getValue(), trc.getDesc());
	}
	
	public String getRow(int row) {
		int columnCount = title_name.length;
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<columnCount; i++) {
			sb.append(getValueAt(row, i));
		}
		return sb.toString();
	}
	
	public String getRowWithoutDes(int row) {
		int columnCount = title_name.length-1;
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<columnCount; i++) {
			sb.append(getValueAt(row, i));
		}
		return sb.toString();
	}
	
	public void removeRow(int row) {
		content.remove(row);
	}

	public void removeRows(int row, int count) {
		for (int i = 0; i < count; i++) {
			if (content.size() > row) {
				content.remove(row);
			}
		}
	}

	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return false;
		}
		return true;
	}

	public void setValueAt(Object value, int row, int col) {
		((Vector) content.get(row)).remove(col);
		((Vector) content.get(row)).add(col, value);
		this.fireTableCellUpdated(row, col);
	}

	public String getColumnName(int col) {
		return title_name[col];
	}

	public int getColumnCount() {
		return title_name.length;
	}

	public int getRowCount() {
		return content.size();
	}

	public Object getValueAt(int row, int col) {
		return ((Vector) content.get(row)).get(col);
	}

	public Class getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}
}